<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>大数据之多范式编程语言 Scala04_流程控制 | Oct25-X</title><meta name="keywords" content="Scala"><meta name="author" content="Oct25-X"><meta name="copyright" content="Oct25-X"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="分支控制 if-else让程序有选择的的执行，分支控制有三种：单分支、双分支、多分支 一、单分支基本语法 123if (条件表达式) &amp;#123;	执行代码块&amp;#125;  二、双分支基本语法 12345if (条件表达式) &amp;#123;	执行代码块1&amp;#125; else &amp;#123;	执行代码块2&amp;#125;  三、多分支基本语法 12345678910if (条件表达式1) &amp;#123;	执">
<meta property="og:type" content="article">
<meta property="og:title" content="大数据之多范式编程语言 Scala04_流程控制">
<meta property="og:url" content="https://oct25-x.atomgit.net/oct25-xxxxx/2024/02/20/47%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala04_%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/index.html">
<meta property="og:site_name" content="Oct25-X">
<meta property="og:description" content="分支控制 if-else让程序有选择的的执行，分支控制有三种：单分支、双分支、多分支 一、单分支基本语法 123if (条件表达式) &amp;#123;	执行代码块&amp;#125;  二、双分支基本语法 12345if (条件表达式) &amp;#123;	执行代码块1&amp;#125; else &amp;#123;	执行代码块2&amp;#125;  三、多分支基本语法 12345678910if (条件表达式1) &amp;#123;	执">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://oct25-x.atomgit.net/oct25-xxxxx/img/cover_img/ctt47.png">
<meta property="article:published_time" content="2024-02-20T04:12:12.000Z">
<meta property="article:modified_time" content="2024-02-24T11:05:43.839Z">
<meta property="article:author" content="Oct25-X">
<meta property="article:tag" content="Scala">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://oct25-x.atomgit.net/oct25-xxxxx/img/cover_img/ctt47.png"><link rel="shortcut icon" href="/oct25-xxxxx/img/logo.jpg"><link rel="canonical" href="https://oct25-x.atomgit.net/oct25-xxxxx/2024/02/20/47%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala04_%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/oct25-xxxxx/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/oct25-xxxxx/',
  algolia: undefined,
  localSearch: {"path":"search.json","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '大数据之多范式编程语言 Scala04_流程控制',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-02-24 19:05:43'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/rightMenu.css"><meta name="generator" content="Hexo 6.1.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/oct25-xxxxx/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/oct25-xxxxx/archives/"><div class="headline">文章</div><div class="length-num">52</div></a></div><div class="data-item"><a href="/oct25-xxxxx/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="data-item"><a href="/oct25-xxxxx/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/oct25-xxxxx/img/top_img/top_bg21.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/oct25-xxxxx/">Oct25-X</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">大数据之多范式编程语言 Scala04_流程控制</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-02-20T04:12:12.000Z" title="发表于 2024-02-20 12:12:12">2024-02-20</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-02-24T11:05:43.839Z" title="更新于 2024-02-24 19:05:43">2024-02-24</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/oct25-xxxxx/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="大数据之多范式编程语言 Scala04_流程控制"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="分支控制-if-else"><a href="#分支控制-if-else" class="headerlink" title="分支控制 if-else"></a>分支控制 if-else</h1><p>让程序有选择的的执行，分支控制有三种：单分支、双分支、多分支</p>
<h2 id="一、单分支"><a href="#一、单分支" class="headerlink" title="一、单分支"></a>一、单分支</h2><p>基本语法</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (条件表达式) &#123;</span><br><span class="line">	执行代码块</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="二、双分支"><a href="#二、双分支" class="headerlink" title="二、双分支"></a>二、双分支</h2><p>基本语法</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (条件表达式) &#123;</span><br><span class="line">	执行代码块<span class="number">1</span></span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">	执行代码块<span class="number">2</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="三、多分支"><a href="#三、多分支" class="headerlink" title="三、多分支"></a>三、多分支</h2><p>基本语法</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (条件表达式<span class="number">1</span>) &#123;</span><br><span class="line">	执行代码块<span class="number">1</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">if</span> (条件表达式<span class="number">2</span>) &#123;</span><br><span class="line">	执行代码块<span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line">   ……</span><br><span class="line"><span class="keyword">else</span> &#123;</span><br><span class="line">	执行代码块n</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="四、if-else-分支控制示例"><a href="#四、if-else-分支控制示例" class="headerlink" title="四、if-else 分支控制示例"></a>四、if-else 分支控制示例</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter04</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> scala.io.<span class="type">StdIn</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala分支控制</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala01_IfElse</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line">    println(<span class="string">&quot;请输入年龄：&quot;</span>)</span><br><span class="line">    <span class="keyword">val</span> age: <span class="type">Int</span> = <span class="type">StdIn</span>.readInt()</span><br><span class="line">    <span class="comment">// 单分支</span></span><br><span class="line">    <span class="keyword">if</span> ( age &gt; <span class="number">18</span>) &#123;</span><br><span class="line">      println(<span class="string">&quot;成年&quot;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    println(<span class="string">&quot;=== === === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 双分支</span></span><br><span class="line">    <span class="keyword">if</span> ( age &gt; <span class="number">18</span> ) &#123;</span><br><span class="line">      println(<span class="string">&quot;成年&quot;</span>)</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      println(<span class="string">&quot;未成年&quot;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    println(<span class="string">&quot;=== === === === === === === === &quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 多分支</span></span><br><span class="line">    <span class="keyword">if</span> ( age &lt;= <span class="number">6</span> ) &#123;</span><br><span class="line">      println(<span class="string">&quot;童年&quot;</span>)</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> ( age &lt; <span class="number">18</span> )&#123;</span><br><span class="line">      println(<span class="string">&quot;青少年&quot;</span>)</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (age &lt; <span class="number">35</span>) &#123;</span><br><span class="line">      println(<span class="string">&quot;青年&quot;</span>)</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (age &lt; <span class="number">60</span>) &#123;</span><br><span class="line">      println(<span class="string">&quot;中年&quot;</span>)</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      println(<span class="string">&quot;老年&quot;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="五、Scala中表达式是有返回值的"><a href="#五、Scala中表达式是有返回值的" class="headerlink" title="五、Scala中表达式是有返回值的"></a>五、Scala中表达式是有返回值的</h2><p>Scala中if-else表达式是有返回值的，具体返回值取决于<strong>满足条件的代码体的最后一行内容</strong>。</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter04</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> scala.io.<span class="type">StdIn</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala表达式返回值</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala02_IfElseReturn</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line">    <span class="comment">// （1）Scala中，表达式是有返回值的</span></span><br><span class="line">    <span class="comment">// （2）表达式返回值是表达式的最后一行的值</span></span><br><span class="line">    <span class="comment">// （3）声明表达式返回值类型为Unit，可以给返回值，但是输出()</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 以if-else为例</span></span><br><span class="line">    println(<span class="string">&quot;请输入年龄：&quot;</span>)</span><br><span class="line">    <span class="keyword">val</span> age: <span class="type">Int</span> = <span class="type">StdIn</span>.readInt()</span><br><span class="line">    <span class="keyword">val</span> result: <span class="type">String</span> = <span class="keyword">if</span> (age &lt;= <span class="number">6</span>) &#123;</span><br><span class="line">      <span class="string">&quot;童年&quot;</span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (age &lt; <span class="number">18</span>) &#123;</span><br><span class="line">      <span class="string">&quot;青少年&quot;</span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (age &lt; <span class="number">35</span>) &#123;</span><br><span class="line">      <span class="string">&quot;青年&quot;</span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (age &lt; <span class="number">60</span>) &#123;</span><br><span class="line">      <span class="string">&quot;中年&quot;</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="string">&quot;老年&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">    println(<span class="string">&quot;result = &quot;</span> + result)</span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Scala 中<strong>返回值类型不一致</strong>，<strong>取它们共同的祖先类型</strong>。</p>
<p><img src="/oct25-xxxxx/img/scala/06_if-else%E8%BF%94%E5%9B%9E%E5%80%BC.png"></p>
<p><code>Java 中的三元运算符，在 Scala 中，可以使用 if-else 实现</code></p>
<p>如果<strong>大括号{}内的逻辑代码只有一行</strong>，<strong>大括号可以省略</strong>。如果省略大括号，if 只对最近的一行逻辑代码起作用。</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 使用if-else实现Java中的三元运算符</span></span><br><span class="line"><span class="keyword">val</span> result2 = <span class="keyword">if</span> (age &gt;= <span class="number">18</span>) <span class="string">&quot;成年&quot;</span> <span class="keyword">else</span> <span class="string">&quot;未成年&quot;</span></span><br></pre></td></tr></table></figure>

<h1 id="嵌套分支"><a href="#嵌套分支" class="headerlink" title="嵌套分支"></a>嵌套分支</h1><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">val</span> result3: <span class="type">String</span> = <span class="keyword">if</span> (age &lt; <span class="number">18</span>) &#123;</span><br><span class="line">    <span class="string">&quot;未成年&quot;</span></span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (age &gt;= <span class="number">18</span> &amp;&amp; age &lt; <span class="number">30</span>) &#123;</span><br><span class="line">        <span class="string">&quot;青年年&quot;</span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (age &lt; <span class="number">60</span>) &#123;</span><br><span class="line">        <span class="string">&quot;中年&quot;</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="string">&quot;老年&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">println(result3)</span><br></pre></td></tr></table></figure>

<h1 id="Switch-分支结构"><a href="#Switch-分支结构" class="headerlink" title="Switch 分支结构"></a>Switch 分支结构</h1><p>在 Scala 中没有 Switch，而是使用<strong>模式匹配</strong>来处理，后面讲解。</p>
<h1 id="For-循环控制"><a href="#For-循环控制" class="headerlink" title="For 循环控制"></a>For 循环控制</h1><h2 id="一、范围数据循环（to）"><a href="#一、范围数据循环（to）" class="headerlink" title="一、范围数据循环（to）"></a>一、范围数据循环（to）</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// to：左右闭合（1-5）</span></span><br><span class="line"><span class="comment">//for (i &lt;- 1 to 5) &#123;</span></span><br><span class="line"><span class="keyword">for</span> (i &lt;- <span class="number">1.</span>to(<span class="number">5</span>)) &#123;</span><br><span class="line">    println(<span class="string">&quot;abc abc &quot;</span> + i)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 结果：</span></span><br><span class="line">abc abc <span class="number">1</span></span><br><span class="line">abc abc <span class="number">2</span></span><br><span class="line">abc abc <span class="number">3</span></span><br><span class="line">abc abc <span class="number">4</span></span><br><span class="line">abc abc <span class="number">5</span></span><br></pre></td></tr></table></figure>

<p><strong>注：to 也是个方法</strong></p>
<h2 id="二、范围数据循环（until）"><a href="#二、范围数据循环（until）" class="headerlink" title="二、范围数据循环（until）"></a>二、范围数据循环（until）</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// until：左闭右开（1-4）</span></span><br><span class="line"><span class="keyword">for</span> (i &lt;- <span class="number">1</span> until <span class="number">5</span>) &#123;</span><br><span class="line">    println(<span class="string">&quot;abc abc&quot;</span> + i)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 结果：</span></span><br><span class="line">abc abc <span class="number">1</span></span><br><span class="line">abc abc <span class="number">2</span></span><br><span class="line">abc abc <span class="number">3</span></span><br><span class="line">abc abc <span class="number">4</span></span><br></pre></td></tr></table></figure>

<h2 id="三、循环守卫"><a href="#三、循环守卫" class="headerlink" title="三、循环守卫"></a>三、循环守卫</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 循环守卫</span></span><br><span class="line"><span class="keyword">for</span> (i &lt;- <span class="number">1</span> to <span class="number">5</span> <span class="keyword">if</span> i != <span class="number">2</span> ) &#123;</span><br><span class="line">    println(<span class="string">&quot;abc abc &quot;</span> + i)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 等同于</span></span><br><span class="line"><span class="keyword">for</span> (i &lt;- <span class="number">1</span> to <span class="number">5</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span>(i != <span class="number">2</span>) &#123;</span><br><span class="line">        println(<span class="string">&quot;abc abc &quot;</span> + i)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 结果：</span></span><br><span class="line">abc abc <span class="number">1</span></span><br><span class="line">abc abc <span class="number">3</span></span><br><span class="line">abc abc <span class="number">4</span></span><br><span class="line">abc abc <span class="number">5</span></span><br></pre></td></tr></table></figure>

<h2 id="四、循环步长"><a href="#四、循环步长" class="headerlink" title="四、循环步长"></a>四、循环步长</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 循环步长</span></span><br><span class="line"><span class="keyword">for</span> (i &lt;- <span class="number">1</span> to <span class="number">5</span> by <span class="number">2</span>) &#123;</span><br><span class="line">    println(<span class="string">&quot;abc abc &quot;</span> + i)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 结果：</span></span><br><span class="line">abc abc <span class="number">1</span></span><br><span class="line">abc abc <span class="number">3</span></span><br><span class="line">abc abc <span class="number">5</span></span><br></pre></td></tr></table></figure>

<h2 id="五、嵌套循环"><a href="#五、嵌套循环" class="headerlink" title="五、嵌套循环"></a>五、嵌套循环</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 嵌套</span></span><br><span class="line"><span class="keyword">for</span> (i &lt;- <span class="number">1</span> to <span class="number">5</span>; j &lt;- <span class="number">6</span> to <span class="number">10</span>) &#123;</span><br><span class="line">    println(<span class="string">&quot;abc abc &quot;</span> + i + <span class="string">&quot; - &quot;</span> + j)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 等同于双重for循环</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 结果：</span></span><br><span class="line">abc abc <span class="number">1</span> - <span class="number">6</span></span><br><span class="line">abc abc <span class="number">1</span> - <span class="number">7</span></span><br><span class="line">abc abc <span class="number">1</span> - <span class="number">8</span></span><br><span class="line">abc abc <span class="number">1</span> - <span class="number">9</span></span><br><span class="line">abc abc <span class="number">1</span> - <span class="number">10</span></span><br><span class="line">abc abc <span class="number">2</span> - <span class="number">6</span></span><br><span class="line">... ...</span><br><span class="line">abc abc <span class="number">2</span> - <span class="number">10</span></span><br><span class="line">... ... </span><br><span class="line">abc abc <span class="number">5</span> - <span class="number">10</span></span><br></pre></td></tr></table></figure>

<p>注：需使用分号”;”进行分隔</p>
<h2 id="六、引入变量"><a href="#六、引入变量" class="headerlink" title="六、引入变量"></a>六、引入变量</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 循环变量</span></span><br><span class="line"><span class="keyword">for</span> (i &lt;- <span class="number">1</span> to <span class="number">5</span>; j = <span class="number">6</span> - i) &#123;</span><br><span class="line">    println(<span class="string">&quot;abc abc &quot;</span> + i + <span class="string">&quot; - &quot;</span> + j)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 结果：</span></span><br><span class="line">abc abc <span class="number">1</span> - <span class="number">5</span></span><br><span class="line">abc abc <span class="number">2</span> - <span class="number">4</span></span><br><span class="line">abc abc <span class="number">3</span> - <span class="number">3</span></span><br><span class="line">abc abc <span class="number">4</span> - <span class="number">2</span></span><br><span class="line">abc abc <span class="number">5</span> - <span class="number">1</span></span><br></pre></td></tr></table></figure>

<p><strong>注：</strong></p>
<p><strong>1）需使用分号”;”进行分隔；</strong></p>
<p><strong>2）for 中有单一表达式时使用园括号”()”，出现多表达式情况时需使用大括号”{}”，如下所示</strong></p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> &#123;</span><br><span class="line">    i &lt;- <span class="number">1</span> to <span class="number">5</span>; j = <span class="number">6</span> - i</span><br><span class="line">&#125; &#123;</span><br><span class="line">    println(<span class="string">&quot;abc abc &quot;</span> + i + <span class="string">&quot; - &quot;</span> + j)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 上述两种方式，等同于</span></span><br><span class="line"><span class="keyword">for</span> (i &lt;- <span class="number">1</span> to <span class="number">5</span>) &#123;</span><br><span class="line">    j = <span class="number">6</span> - i</span><br><span class="line">    println(<span class="string">&quot;abc abc &quot;</span> + i + <span class="string">&quot; - &quot;</span> + j)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="七、循环返回值-yield"><a href="#七、循环返回值-yield" class="headerlink" title="七、循环返回值 yield"></a>七、循环返回值 yield</h2><p>在 Scala 中，像是 if 表达式是存在返回值的，那 for 循环是否有返回值呢？</p>
<p>写一个 for 循环，然后直接将循环中的变量 i 返回，打印结果：()</p>
<p>也就是说，for 循环的返回值是 Unit</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Scala中for循环返回值</span></span><br><span class="line"><span class="keyword">val</span> res = <span class="keyword">for</span> (i &lt;- <span class="number">1</span> to <span class="number">10</span>) &#123;</span><br><span class="line">    i</span><br><span class="line">&#125;</span><br><span class="line">println(res) <span class="comment">// 返回：()</span></span><br></pre></td></tr></table></figure>

<p>那是不是就说 Scala 中的 for 循环是没有返回值了？</p>
<p>也不能完全这么说，默认情况下，Scala 中的 for 循环确实是没有返回值，或者说返回值为 Unit</p>
<p>但是，可以通过 yield 给循环返回一个集合，如下所示</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 将循环中变量i所有取值封装为集合返回</span></span><br><span class="line"><span class="keyword">val</span> res1 = <span class="keyword">for</span> (i &lt;- <span class="number">1</span> to <span class="number">5</span>) <span class="keyword">yield</span> i</span><br><span class="line">println(res1) <span class="comment">// Vector(1, 2, 3, 4, 5)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 也可以对变量i进行其他操作</span></span><br><span class="line"><span class="keyword">val</span> res2 = <span class="keyword">for</span> (i &lt;- <span class="number">1</span> to <span class="number">5</span>) <span class="keyword">yield</span> i * <span class="number">2</span></span><br><span class="line">println(res2) <span class="comment">// Vector(2, 4, 6, 8, 10)</span></span><br></pre></td></tr></table></figure>

<h2 id="八、倒序打印"><a href="#八、倒序打印" class="headerlink" title="八、倒序打印"></a>八、倒序打印</h2><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> scala.language.postfixOps</span><br><span class="line"></span><br><span class="line"><span class="comment">// 倒序打印</span></span><br><span class="line"><span class="keyword">for</span> (i &lt;- <span class="number">1</span> to <span class="number">5</span> reverse) &#123;</span><br><span class="line">    println(i)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 结果：</span></span><br><span class="line"><span class="number">5</span></span><br><span class="line"><span class="number">4</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="number">1</span></span><br></pre></td></tr></table></figure>

<h1 id="while-和-do…while-循环"><a href="#while-和-do…while-循环" class="headerlink" title="while 和 do…while 循环"></a>while 和 do…while 循环</h1><p><strong>While 和 do..While 的使用和 Java 语言中用法相同。</strong></p>
<p>说明：</p>
<ol>
<li><p>循环条件是返回一个布尔值的表达式</p>
</li>
<li><p>while 循环是先判断再执行语句</p>
</li>
<li><p>与 if 语句不同，while 语句没有返回值，即整个 while 语句的结果是 Unit 类型()</p>
</li>
<li><p>因为 while 中没有返回值，所以当要用该语句来计算并返回结果时，就不可避免的使用变量，而变量需要声明在while循环的外部，那么就等同于循环的内部对外部的变量造成了影响，也就违背了函数式编程的重要思想（输入&#x3D;&gt;函数&#x3D;&gt;输出，不对外界造成影响），所以不推荐使用，而是<strong>推荐使用for循环</strong>。</p>
</li>
</ol>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// while循环</span></span><br><span class="line"><span class="keyword">var</span> i = <span class="number">0</span></span><br><span class="line"><span class="keyword">while</span> (i &lt; <span class="number">10</span>) &#123;</span><br><span class="line">    println(<span class="string">&quot;qwe &quot;</span> + i)</span><br><span class="line">    i += <span class="number">1</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// do-while</span></span><br><span class="line"><span class="keyword">var</span> j = <span class="number">0</span></span><br><span class="line"><span class="keyword">do</span> &#123;</span><br><span class="line">    println(<span class="string">&quot;asd &quot;</span>+ j)</span><br><span class="line">    j += <span class="number">1</span></span><br><span class="line">&#125; <span class="keyword">while</span> (j &lt; <span class="number">10</span>)</span><br></pre></td></tr></table></figure>

<h1 id="循环中断"><a href="#循环中断" class="headerlink" title="循环中断"></a>循环中断</h1><p>Scala 内置控制结构特地去掉了 break 和 continue，是为了更好的适应函数式编程，推荐使用函数式的风格解决 break 和 continue 的功能，而不是一个关键字。<strong>Scala 中使用 breakable 控制结构来实现 break 和 continue 功能。</strong></p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter04</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> scala.util.control.<span class="type">Breaks</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala循环中断</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala06_Break</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line">    <span class="type">Breaks</span>.breakable(</span><br><span class="line">      <span class="keyword">for</span> (i &lt;- <span class="number">1</span> to <span class="number">10</span>) &#123;</span><br><span class="line">        println(i)</span><br><span class="line">        <span class="keyword">if</span> (i == <span class="number">5</span>) <span class="type">Breaks</span>.<span class="keyword">break</span>()</span><br><span class="line">      &#125;</span><br><span class="line">    )</span><br><span class="line">    println(<span class="string">&quot;循环结束... ...&quot;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>可以点到 Breaks.breakable() 和 Breaks.break() 两个方法的源码中看下，可以发现，Scala 中的循环中断是通过异常机制实现</p>
<p><strong>Breaks.breakable() 方法，通过 try-catch 捕获异常</strong></p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">breakable</span></span>(op: =&gt; <span class="type">Unit</span>): <span class="type">Unit</span> =</span><br><span class="line">	<span class="keyword">try</span> op <span class="keyword">catch</span> &#123; <span class="keyword">case</span> ex: <span class="type">BreakControl</span> <span class="keyword">if</span> ex eq breakException =&gt; &#125;</span><br></pre></td></tr></table></figure>

<p><strong>Breaks.break() 方法，抛出一个 breakException 异常</strong></p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">break</span></span>(): <span class="type">Nothing</span> = <span class="keyword">throw</span> breakException</span><br></pre></td></tr></table></figure>

<blockquote>
<p>补充：</p>
<p>当导包时，使用 <code>import scala.util.control.Breaks._</code> 可以理解为 Java 中的 *，这时 Breaks.breakable() 和 Breaks.break() 就可以把 Breaks 省略，直接使用 breakable() 和 break() 就可以。</p>
</blockquote>
<h1 id="多重循环"><a href="#多重循环" class="headerlink" title="多重循环"></a>多重循环</h1><p><strong>打印99乘法表</strong></p>
<p><strong>打印星号*的等腰三角形</strong></p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.example.chapter04</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Description TODO Scala for循环练习</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">object</span> <span class="title">Scala04_Practice</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">main</span></span>(args: <span class="type">Array</span>[<span class="type">String</span>]): <span class="type">Unit</span> = &#123;</span><br><span class="line">    test2()</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 输出99乘法表</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">test1</span></span>(): <span class="type">Unit</span> = &#123;</span><br><span class="line">    <span class="keyword">for</span> (i &lt;- <span class="number">1</span> to <span class="number">9</span>; j &lt;- <span class="number">1</span> to i) &#123;</span><br><span class="line">      print(<span class="string">s&quot;<span class="subst">$&#123;j&#125;</span> * <span class="subst">$&#123;i&#125;</span> = <span class="subst">$&#123; i * j&#125;</span>\t&quot;</span>)</span><br><span class="line">      <span class="keyword">if</span> (i == j) println()</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 等腰三角形</span></span><br><span class="line"><span class="comment">   * 第一层一个星号，第二层两个.... 第九层九个星号</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">test2</span></span>(): <span class="type">Unit</span> = &#123;</span><br><span class="line"><span class="comment">//    for (i &lt;- 1 to 9) &#123;</span></span><br><span class="line"><span class="comment">//      val stars = 2 * i - 1</span></span><br><span class="line"><span class="comment">//      val spaces = (17 - (2 * i -1)) / 2</span></span><br><span class="line"><span class="comment">//      println(&quot; &quot; * spaces + &quot;*&quot; * stars)</span></span><br><span class="line"><span class="comment">//    &#125;</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (i &lt;- <span class="number">1</span> to <span class="number">9</span>; stars = <span class="number">2</span> * i - <span class="number">1</span>; spaces = (<span class="number">17</span> - (<span class="number">2</span> * i - <span class="number">1</span>)) / <span class="number">2</span>) &#123;</span><br><span class="line">      println(<span class="string">&quot; &quot;</span> * spaces + <span class="string">&quot;*&quot;</span> * stars)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Oct25-X</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://oct25-x.atomgit.net/oct25-xxxxx/2024/02/20/47%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala04_%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/">https://oct25-x.atomgit.net/oct25-xxxxx/2024/02/20/47%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala04_%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://oct25-x.atomgit.net/oct25-xxxxx" target="_blank">Oct25-X</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/oct25-xxxxx/tags/Scala/">Scala</a></div><div class="post_share"><div class="social-share" data-image="/oct25-xxxxx/img/cover_img/ctt47.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/"><img class="prev-cover" src="/oct25-xxxxx/img/cover_img/ctt48.png" onerror="onerror=null;src='/oct25-xxxxx/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">大数据之多范式编程语言 Scala05_函数式编程</div></div></a></div><div class="next-post pull-right"><a href="/oct25-xxxxx/2024/02/19/46%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala03_%E8%BF%90%E7%AE%97%E7%AC%A6/"><img class="next-cover" src="/oct25-xxxxx/img/cover_img/ctt46.png" onerror="onerror=null;src='/oct25-xxxxx/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">大数据之多范式编程语言 Scala03_运算符</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/oct25-xxxxx/2024/02/18/44%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala01_%E5%85%A5%E9%97%A8/" title="大数据之多范式编程语言 Scala01_入门"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt44.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-18</div><div class="title">大数据之多范式编程语言 Scala01_入门</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/19/46%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala03_%E8%BF%90%E7%AE%97%E7%AC%A6/" title="大数据之多范式编程语言 Scala03_运算符"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt46.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-19</div><div class="title">大数据之多范式编程语言 Scala03_运算符</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/18/45%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala02_%E5%8F%98%E9%87%8F%E5%92%8C%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/" title="大数据之多范式编程语言 Scala02_变量和数据类型"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt45.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-18</div><div class="title">大数据之多范式编程语言 Scala02_变量和数据类型</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="大数据之多范式编程语言 Scala05_函数式编程"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt48.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-20</div><div class="title">大数据之多范式编程语言 Scala05_函数式编程</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt49.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-21</div><div class="title">大数据之多范式编程语言 Scala06_面向对象</div></div></a></div><div><a href="/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/" title="大数据之多范式编程语言 Scala07_集合"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt50.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-02-22</div><div class="title">大数据之多范式编程语言 Scala07_集合</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/oct25-xxxxx/img/avatar.jpg" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Oct25-X</div><div class="author-info__description"></div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/oct25-xxxxx/archives/"><div class="headline">文章</div><div class="length-num">52</div></a></div><div class="card-info-data-item"><a href="/oct25-xxxxx/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="card-info-data-item"><a href="/oct25-xxxxx/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%88%86%E6%94%AF%E6%8E%A7%E5%88%B6-if-else"><span class="toc-number">1.</span> <span class="toc-text">分支控制 if-else</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E5%8D%95%E5%88%86%E6%94%AF"><span class="toc-number">1.1.</span> <span class="toc-text">一、单分支</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%8F%8C%E5%88%86%E6%94%AF"><span class="toc-number">1.2.</span> <span class="toc-text">二、双分支</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E5%A4%9A%E5%88%86%E6%94%AF"><span class="toc-number">1.3.</span> <span class="toc-text">三、多分支</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E3%80%81if-else-%E5%88%86%E6%94%AF%E6%8E%A7%E5%88%B6%E7%A4%BA%E4%BE%8B"><span class="toc-number">1.4.</span> <span class="toc-text">四、if-else 分支控制示例</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94%E3%80%81Scala%E4%B8%AD%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%98%AF%E6%9C%89%E8%BF%94%E5%9B%9E%E5%80%BC%E7%9A%84"><span class="toc-number">1.5.</span> <span class="toc-text">五、Scala中表达式是有返回值的</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%B5%8C%E5%A5%97%E5%88%86%E6%94%AF"><span class="toc-number">2.</span> <span class="toc-text">嵌套分支</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Switch-%E5%88%86%E6%94%AF%E7%BB%93%E6%9E%84"><span class="toc-number">3.</span> <span class="toc-text">Switch 分支结构</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#For-%E5%BE%AA%E7%8E%AF%E6%8E%A7%E5%88%B6"><span class="toc-number">4.</span> <span class="toc-text">For 循环控制</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E8%8C%83%E5%9B%B4%E6%95%B0%E6%8D%AE%E5%BE%AA%E7%8E%AF%EF%BC%88to%EF%BC%89"><span class="toc-number">4.1.</span> <span class="toc-text">一、范围数据循环（to）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E8%8C%83%E5%9B%B4%E6%95%B0%E6%8D%AE%E5%BE%AA%E7%8E%AF%EF%BC%88until%EF%BC%89"><span class="toc-number">4.2.</span> <span class="toc-text">二、范围数据循环（until）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E5%BE%AA%E7%8E%AF%E5%AE%88%E5%8D%AB"><span class="toc-number">4.3.</span> <span class="toc-text">三、循环守卫</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E5%BE%AA%E7%8E%AF%E6%AD%A5%E9%95%BF"><span class="toc-number">4.4.</span> <span class="toc-text">四、循环步长</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E5%B5%8C%E5%A5%97%E5%BE%AA%E7%8E%AF"><span class="toc-number">4.5.</span> <span class="toc-text">五、嵌套循环</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%AD%E3%80%81%E5%BC%95%E5%85%A5%E5%8F%98%E9%87%8F"><span class="toc-number">4.6.</span> <span class="toc-text">六、引入变量</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%83%E3%80%81%E5%BE%AA%E7%8E%AF%E8%BF%94%E5%9B%9E%E5%80%BC-yield"><span class="toc-number">4.7.</span> <span class="toc-text">七、循环返回值 yield</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%AB%E3%80%81%E5%80%92%E5%BA%8F%E6%89%93%E5%8D%B0"><span class="toc-number">4.8.</span> <span class="toc-text">八、倒序打印</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#while-%E5%92%8C-do%E2%80%A6while-%E5%BE%AA%E7%8E%AF"><span class="toc-number">5.</span> <span class="toc-text">while 和 do…while 循环</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%BE%AA%E7%8E%AF%E4%B8%AD%E6%96%AD"><span class="toc-number">6.</span> <span class="toc-text">循环中断</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%A4%9A%E9%87%8D%E5%BE%AA%E7%8E%AF"><span class="toc-number">7.</span> <span class="toc-text">多重循环</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/24/52%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala09_%E9%9A%90%E5%BC%8F%E8%BD%AC%E6%8D%A2%E3%80%81%E5%BC%82%E5%B8%B8%E5%92%8C%E6%B3%9B%E5%9E%8B/" title="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型"><img src="/oct25-xxxxx/img/cover_img/ctt52.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/24/52%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala09_%E9%9A%90%E5%BC%8F%E8%BD%AC%E6%8D%A2%E3%80%81%E5%BC%82%E5%B8%B8%E5%92%8C%E6%B3%9B%E5%9E%8B/" title="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型">大数据之多范式编程语言 Scala09_隐式转换、异常和泛型</a><time datetime="2024-02-24T04:55:34.000Z" title="发表于 2024-02-24 12:55:34">2024-02-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/24/51%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala08_%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D/" title="大数据之多范式编程语言 Scala08_模式匹配"><img src="/oct25-xxxxx/img/cover_img/ctt51.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala08_模式匹配"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/24/51%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala08_%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D/" title="大数据之多范式编程语言 Scala08_模式匹配">大数据之多范式编程语言 Scala08_模式匹配</a><time datetime="2024-02-24T00:46:12.000Z" title="发表于 2024-02-24 08:46:12">2024-02-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/" title="大数据之多范式编程语言 Scala07_集合"><img src="/oct25-xxxxx/img/cover_img/ctt50.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala07_集合"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/" title="大数据之多范式编程语言 Scala07_集合">大数据之多范式编程语言 Scala07_集合</a><time datetime="2024-02-22T08:16:10.000Z" title="发表于 2024-02-22 16:16:10">2024-02-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象"><img src="/oct25-xxxxx/img/cover_img/ctt49.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala06_面向对象"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象">大数据之多范式编程语言 Scala06_面向对象</a><time datetime="2024-02-21T07:01:10.000Z" title="发表于 2024-02-21 15:01:10">2024-02-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="大数据之多范式编程语言 Scala05_函数式编程"><img src="/oct25-xxxxx/img/cover_img/ctt48.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala05_函数式编程"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="大数据之多范式编程语言 Scala05_函数式编程">大数据之多范式编程语言 Scala05_函数式编程</a><time datetime="2024-02-20T12:05:11.000Z" title="发表于 2024-02-20 20:05:11">2024-02-20</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/oct25-xxxxx/img/top_img/top_bg21.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2024 By Oct25-X</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">本地搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div id="rightMenu"><div class="rightMenu-group rightMenu-small"><div class="rightMenu-item" id="menu-backward"><i class="fa-solid fa-arrow-left"></i></div><div class="rightMenu-item" id="menu-forward"><i class="fa-solid fa-arrow-right"></i></div><div class="rightMenu-item" id="menu-refresh"><i class="fa-solid fa-arrow-rotate-right"></i></div><div class="rightMenu-item" id="menu-home"><i class="fa-solid fa-house"></i></div></div><div class="rightMenu-group rightMenu-line rightMenuOther"><a class="rightMenu-item menu-link" href="/archives/"><i class="fa-solid fa-archive"></i><span>文章归档</span></a><a class="rightMenu-item menu-link" href="/categories/"><i class="fa-solid fa-folder-open"></i><span>文章分类</span></a><a class="rightMenu-item menu-link" href="/tags/"><i class="fa-solid fa-tags"></i><span>文章标签</span></a></div><div class="rightMenu-group rightMenu-line rightMenuNormal"><a class="rightMenu-item" id="menu-translate"><i class="fa-solid fa-earth-asia"></i><span>繁简切换</span></a><div class="rightMenu-item" id="menu-darkmode"><i class="fa-solid fa-moon"></i><span>切换模式</span></div></div></div><div id="rightmenu-mask"></div><div><script src="/oct25-xxxxx/js/utils.js"></script><script src="/oct25-xxxxx/js/main.js"></script><script src="/oct25-xxxxx/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="/oct25-xxxxx/js/search/local-search.js"></script><div class="js-pjax"></div><script defer src="https://npm.elemecdn.com/jquery@latest/dist/jquery.min.js"></script><script defer data-pjax src="/js/rightMenu.js"></script><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>